<!-- /src/views/ArticleDetail.vue -->
<template>
    <div class="article-detail-page">
        <div class="article-detail">
            <h1>{{ article.title }}</h1>
            <div class="article-meta">
            <span>{{ article.category?.name || '未分类' }}</span>
            <span>{{ formatDate(article.published_at) }}</span>
            </div>
            <img v-if="article.cover" :src="getCoverUrl(article.cover)" alt="article.title" />
            <div v-html="article.content"></div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import frontApi from '@/api/front';
import { formatDate } from '@/utils/format';

const route = useRoute();
const article = ref({});

const getCoverUrl = (path) => {
  if (!path) return ''
  if (/^https?:\/\//.test(path)) return path
  const base = import.meta.env.VITE_API_BASE_URL?.replace(/\/$/, '') || ''
  return path.startsWith('storage/')
    ? `${base}/${path}`
    : `${base}/storage/${path}`
}

const fetchArticle = async () => {
try {
    const res = await frontApi.getArticle(route.params.id);
    article.value = {
    ...res.data,
    };
} catch (err) {
    console.error('获取文章失败', err);
}
};

onMounted(fetchArticle);
</script>

<style scoped lang="scss">
/* 文章详情页面 */
.article-detail-page {
  background: #fafbfc;
  min-height: 100vh;
  padding: 48px 0 40px 0;
}

.article-detail {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  padding: 40px 32px;
  max-width: 900px;
  margin: 0 auto;
}

.article-detail h1 {
  font-size: 32px;
  font-weight: 700;
  color: #222;
  margin-bottom: 24px;
}

.article-detail .meta {
  color: #888;
  font-size: 15px;
  margin-bottom: 24px;
}

.article-detail :deep(h2) {
  font-size: 22px;
  color: #222;
  margin: 32px 0 16px 0;
}

.article-detail :deep(h3) {
  font-size: 18px;
  color: #222;
  margin: 24px 0 12px 0;
}

.article-detail :deep(p) {
  color: #555;
  font-size: 16px;
  line-height: 1.8;
  margin-bottom: 16px;
}

/* 元信息（分类和日期） */
.article-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: #64748b;
  margin-bottom: 24px;
}

.article-meta span {
  display: inline-flex;
  align-items: center;
}

.article-meta span:first-child {
  background: #e0f2fe;
  color: #2563eb;
  padding: 4px 12px;
  border-radius: 6px;
  font-weight: 500;
}

/* 封面图片 */
.article-detail img {
  width: 100%;
  max-height: 400px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 24px;
  display: block;
}

/* 正文中的列表 */
.article-detail :deep(ul),
.article-detail :deep(ol) {
  margin-bottom: 16px;
  padding-left: 24px;
}

.article-detail :deep(li) {
  margin-bottom: 8px;
}

/* 正文中的代码块 */
.article-detail :deep(pre) {
  background: #f8fafc;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin-bottom: 16px;
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 14px;
  color: #334155;
}

/* 正文中的内联代码 */
.article-detail :deep(code) {
  background: #f1f5f9;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 14px;
  color: #d63515;
}

/* 正文中的图片 */
.article-detail :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 16px 0;
  display: block;
}

/* 正文中的链接 */
.article-detail :deep(a) {
  color: #2563eb;
  text-decoration: none;
  transition: color 0.2s ease;
}

.article-detail :deep(a:hover) {
  color: #1d4ed8;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .article-detail {
    padding: 20px 8px;
    border-radius: 12px;
  }
  .article-detail h1 {
    font-size: 22px;
  }

  .article-detail img {
    max-height: 300px;
  }

  .article-detail :deep(.content-body) {
    font-size: 15px;
  }

  .article-detail :deep(h2) {
    font-size: 22px;
  }

  .article-detail :deep(h3) {
    font-size: 18px;
  }
}
</style>